require(['util', 'validator'], function () {

    var $safeBehavCommentForm = $("#safeBehavCommentForm");

    var seleObj = {},
        corpId = "";

    //左侧树点击事件
    commonFun.leftTreeNodeClickObj['funCheckBehaviorCommentSet'] = function (event, treeId, treeNode) {
        if (treeNode.type === 'CAR' || treeNode.type === 'DRIVER') {
            $.messager.alert("提示", "请选择组织机构或线路，进行参数设置。", "info");
            return;
        }
        seleObj = treeNode;
        corpId = treeNode.corpId;

        showInterface();
    };

    //根据后台返回的数据，动态生成界面
    var showInterface = function () {
        $.ajaxExt({
            url: "/driverComment/searchDriverCommentInfo.htm",
            data: {
                "corpId": corpId,
                "organId": seleObj.id,
                "qcommentTypeCodes": new Array("CMT00000").toString(),
                "qcodeValue": "CMT00001"
            }
        }).done(function (data) {
            var currData = data;
            //显示界面
            $('#u_tip').remove();
            $safeBehavCommentForm.empty();
            $("#safeBehavCommentBnt").show();
            //根据数据渲染界面
            var seleObjId = seleObj.id,
                currObjId = currData[0].organId;

            $safeBehavCommentForm.append("<h1 class='line-title'>【" + seleObj.name + "】的安全行为评语设置信息</h1>"
            + "<input type='hidden' name='corpId' value='" + corpId + "'/>"
            + "<input type='hidden' name='seleId' value='" + seleObjId + "'/>"
            + "<input type='hidden' name='currId' value='" + currObjId + "'/>");
            $.each(currData, function (n, entity) {
                var params = window.parent.GLOBAL_PARAMS.DICT_CACHE[entity.paramCode];
                var paramDsc = "";
                $.each(params, function (n, param) {
                    paramDsc = paramDsc + param.value + ", ";
                });
                paramDsc = paramDsc.substr(0, paramDsc.length - 2);

                var preview = entity.commentCntnt.format({
                    '0': '一分公司',
                    '1': '18路',
                    '2': '张三',
                    '3': '2014-10-01~2014-11-01',
                    '4': '98'
                });

                var content ="<input type='hidden' name='commentIds' value='" + entity.commentId + "'/>"
                    + "<div class='user-info-box'>"
                    + "<h2 class='title-style1 mt20'><span>评语等级：" + entity.commentGrade + "</span></h2>"
                    + "<ul class='form-grid form-grid2 form-grid-nowrap clearfix'>"
                    + "<li>"
                    + "<label class='label-text'>评语名称：</label><input type='text' name='commentGradeDscs' class='input-text' style='width:90px;' maxlength='16' data-validator='{required:true}' placeholder='请输入评语名称' value='" + entity.commentGradeDsc + "'/>"
                    + "</li>"
                    + "<li>"
                    + "<label class='label-text'>等级范围：</label><input type='text' name='beginValues' class='input-text' style='width:100px;' data-validator='{required:true,number:true}' placeholder='请输入开始值' value='" + entity.beginValue + "'/> ~ <input type='text' name='endValues' class='input-text' style='width:100px;' data-validator='{required:true,number:true}' placeholder='请输入结束值' value='" + entity.endValue + "'/>"
                    + "</li>"
                    + "<li class='li-textarea mb0'>"
                    + "<label class='label-text vt'>评语内容：</label><textarea class='vt' name='commentCntnts' rows='3' cols='80' data-validator='{required:true}' placeholder='根据参数，设置评语内容。'>" + entity.commentCntnt + "</textarea>"
                    + "</li>"
                    + "<li class='mt5 h-auto'><label class='label-text'>参数说明：</label>"
                    + "<span class='span-text c-999'>" + paramDsc + "</span>"
                    + "</li>"
                    + "<li class='mb0 mt10 h-auto wrap' style='width: 800px;'>"
                    + "<label class='label-text'>预览效果：</label><span class='comment-preview'>" + preview + "</span>"
                    + "</li>"
                    + "<li class='mt5 h-auto'><label class='label-text'>备注：</label>"
                    + "<input type='text' name='remarks' class='input-text' maxlength='32' style='width:498px;' placeholder='输入备注信息。' value='" + entity.remark + "'/>"
                   ;+ "</li>"
                    + "</ul>"
                    + "</div>"
                $safeBehavCommentForm.append(content);
            });
            $('textarea[name=commentCntnts]').on("change", function () {
                var preview = $(this).val().format({
                    '0': '一分公司',
                    '1': '18路',
                    '2': '张三',
                    '3': '2014-10-01~2014-11-01',
                    '4': '98'
                });
                $(this).parent().parent().find(".comment-preview").html(preview);
            });
        });
    };

    $("#safeBehavComment_update").on("click", function () {
        if($.validator($('#safeBehavCommentForm'))){
            $.messager.confirm("是否修改", "确定修改该机构的安全行为评语设置信息？", function (r) {
                if (r) {
                    var formData = $safeBehavCommentForm.serialize();
                    $.ajaxExt({
                        url: "/driverComment/uptDriverCommentInfo.htm",
                        data: formData
                    }).done(function (data) {
                    });
                }
            });
        }
    });

    $("#safeBehavComment_reset").on("click", function () {
        showInterface();
    });

    //页面打开自动查询
    var nodes = commonFun.getLeftTreeSelectedNodes();
	if(nodes.length<1||nodes[0].type==="LINE"){
		seleObj = nodes[0];
        corpId = nodes[0].corpId;
        showInterface();
	}
    
})